/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the lkyh.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.lkyh.mes.codegen.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkyh.mes.codegen.entity.GenColumnInfo;
import com.lkyh.mes.codegen.entity.GenPageInfo;
import com.lkyh.mes.codegen.entity.GenVerifyInfo;
import com.lkyh.mes.codegen.mapper.GenColumnInfoMapper;
import com.lkyh.mes.codegen.service.GenColumnInfoService;
import com.lkyh.mes.codegen.service.GenPageInfoService;
import com.lkyh.mes.codegen.service.GenVerifyInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 表单管理
 *
 * @author lengleng
 * @date 2019-08-12 15:55:35
 */
@Service
@AllArgsConstructor
public class GenColumnInfoServiceImpl extends ServiceImpl<GenColumnInfoMapper, GenColumnInfo> implements GenColumnInfoService {

	private final GenPageInfoService genPageInfoService;

	private final GenVerifyInfoService genVerifyInfoService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean save(GenColumnInfo genColumnInfo) {
		super.save(genColumnInfo);
		int order = 0;
		List<GenPageInfo> list = genPageInfoService.list(Wrappers.lambdaQuery(new GenPageInfo().setTableId(genColumnInfo.getTableId())).orderByDesc(GenPageInfo::getOrder));
		if (CollUtil.isNotEmpty(list)) {
			order = CollUtil.getFirst(list).getOrder() + 1;
		}
		genPageInfoService.save(new GenPageInfo().setTableId(genColumnInfo.getTableId()).setName(genColumnInfo.getRemark())
				.setColumnId(genColumnInfo.getId()).setOrder(order));
		genVerifyInfoService.save(new GenVerifyInfo().setTableId(genColumnInfo.getTableId()).setColumnId(genColumnInfo.getId()));
		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveBatch(Collection<GenColumnInfo> entityList) {
		super.saveBatch(entityList);
		List<GenPageInfo> genPageInfoList = new ArrayList<>();
		List<GenVerifyInfo> genVerifyInfoList = new ArrayList<>();

		List<GenColumnInfo> list = CollUtil.newArrayList(entityList).stream().filter(i -> !"Y".equals(i.getHideConf())).collect(Collectors.toList());
		for (int i = 0; i < list.size(); i++) {

			genPageInfoList.add(new GenPageInfo().setTableId(list.get(i).getTableId()).setColumnId(list.get(i).getId()).setOrder(i));
			genVerifyInfoList.add(new GenVerifyInfo().setTableId(list.get(i).getTableId()).setColumnId(list.get(i).getId()));
		}
		genPageInfoService.saveBatch(genPageInfoList);
		genVerifyInfoService.saveBatch(genVerifyInfoList);
		return true;
	}
}
